package passengers.demo;
import important.demo.ImportantListMainActivity;

import java.util.ArrayList;

import presence.demo.R;
import database.demo.DataBaseWrapperPassengers;
import database.demo.PassengersOperations;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;


/**
 * saving passenger's details
 */
public class PassListMainActivity extends Activity {
private PassengersOperations passengerDBoperation;	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.passengers_list);
		passengerDBoperation = new PassengersOperations(this);
		passengerDBoperation.open();
		populatePassengerList();		
	}
	
	@Override
	protected void onResume() {
	   super.onResume();
	   this.onCreate(null);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.passengers_list, menu);
        return true;
    }
	
	public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.item1:
        	Intent newPass=new Intent(passengers.demo.PassListMainActivity.this,NewPassMainActivity.class);
        	//Toast.makeText(getApplicationContext(),getMax(), Toast.LENGTH_LONG).show();
        	//newPass.putExtra("maxID",getMax());
		    startActivity(newPass);
		    return true;
        case R.id.item2:
        	Intent multiSMS=new Intent(passengers.demo.PassListMainActivity.this,MultipleSmsMainActivity.class);
		    startActivity(multiSMS);
		    return true;
        case R.id.item3:
        	Intent multiMail=new Intent(passengers.demo.PassListMainActivity.this,MultipleMailMainActivity.class);
		    startActivity(multiMail);
		    return true;
        default:
          return super.onOptionsItemSelected(item);
        } 
    } 

	//get all passengers from DB
	private void populatePassengerList() {
		
		final Cursor cursor = passengerDBoperation.getAllPassengers();
		
		// Allow activity to manage lifetime of the cursor.
		// DEPRECATED! Runs on the UI thread, OK for small/short queries.
		startManagingCursor(cursor);
		
		// Setup mapping from cursor to view fields:
		String[] fromFieldNames = new String[] 
				{DataBaseWrapperPassengers.PASSENGER_ID,DataBaseWrapperPassengers.PASSENGER_LASTNAME, DataBaseWrapperPassengers.PASSENGER_FIRSTNAME};
		int[] toViewIDs = new int[]
				{R.id.idText,R.id.dname_text, R.id.textFirst};
		
		// Create adapter to may columns of the DB onto elemesnt in the UI.
		SimpleCursorAdapter myCursorAdapter = 
				new SimpleCursorAdapter(
						this,		// Context
						R.layout.pass_det_item_view,	// Row layout template
						cursor,					// cursor (set of DB records to map)
						fromFieldNames,			// DB Column names
						toViewIDs				// View IDs to put information in
						);	
		
		// Set the adapter and listener for the list view
		ListView myList = (ListView) findViewById(R.id.passListView);
		
		myList.setOnItemClickListener(new OnItemClickListener() {
			  @Override
			  public void onItemClick(AdapterView<?> parent, View view,
			    int position, long id) {
			    //Toast.makeText(getApplicationContext(),"Click ListItem Number " + position, Toast.LENGTH_LONG).show();
				cursor.moveToFirst();
				cursor.moveToPosition(position);
				String tmpID = cursor.getString(0).toString();
				//Toast.makeText(getApplicationContext(),tmpID, Toast.LENGTH_LONG).show();
				Intent intent =new Intent(passengers.demo.PassListMainActivity.this,SpecificPassMainActivity.class);
			    intent.putExtra("tmpID", tmpID);
				startActivity(intent);	
			  }
			});
		myList.setAdapter(myCursorAdapter);
		int count = myList.getAdapter().getCount();
		if (count <1){
			Toast.makeText(PassListMainActivity.this,
					"No available passengers", Toast.LENGTH_SHORT).show();
		}
	}
	
	
			
	@Override
	protected void onDestroy() {
		super.onDestroy();	
		passengerDBoperation.close();
	}
}
